package com.winit.openapi.apiadapter.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.ims.spi.inventory.InventoryService;
import com.winit.ims.spi.inventory.command.PageInventorySize4OpenApiCommand;
import com.winit.ims.spi.inventory.command.PageOverseaInventoriesCommand;
import com.winit.ims.spi.inventory.command.pageInventoryDetailCommand;
import com.winit.ims.spi.inventory.vo.PageInventoryDetailVo;
import com.winit.ims.spi.inventory.vo.PageOverseaInventoryVo;
import com.winit.ims.spi.inventory.vo.PageQueryInventorySizeVo;
import com.winit.openapi.apiadapter.controller.builder.WarehouseInventoryBuilder;
import com.winit.openapi.apiadapter.controller.validator.WarehouseInventoryValidator;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ups.spi.warehouse.WarehouseService;

/**
 * AD查询仓库库存以及DOI接口适配
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * minghao.deng 	1.0  		2018年3月21日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
public class WarehouseInventoryAdapteController extends BaseController {

    @Resource
    private InventoryService          inventoryService;

    @Resource
    private WarehouseService          upsWarehouseService;

    @Resource
    private WarehouseInventoryBuilder warehouseInventoryBuilder;

    /**
     * 海外仓总库存查询接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryWarehouseStorage")
    public String queryWarehouseStorage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());

        WarehouseInventoryValidator.validateQueryWarehouseStorage(json);

        PageInventorySize4OpenApiCommand command = warehouseInventoryBuilder.buildQueryWarehouseStorageCommand(json,
            requestMsg.getPlatform());

        Page<PageQueryInventorySizeVo> page = inventoryService.pageInventoryAndSize4OpanApi(command);

        Map<String, Object> data = new HashMap<String, Object>();

        if (page != null && CollectionUtils.isNotEmpty(page.content)) {
            data.put("list", warehouseInventoryBuilder.buildQueryWarehouseStorageResult(page.content, json));
        }

        if (page != null && page.getPageable() != null) {

            data.put("currentPageNum", page.getPageable().getPageNumber());
            data.put("currentPageSize", page.getPageable().getPageSize());
            data.put("total", page.getTotalElements());
        } else {
            data.put("currentPageNum", 0);
            data.put("currentPageSize", 0);
            data.put("total", 0);
        }
        
        responseMsg.setData(data);

        return API_ADAPTED_SUCCESS;

    }

    /**
     * 海外仓总库存查询接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryProductInventoryDetail")
    public String queryProductInventoryDetail(@ModelAttribute RequestMsg requestMsg,
                                              @ModelAttribute ResponseMsg responseMsg) {

        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        WarehouseInventoryValidator.validateProductInventoryDatail(json);

        pageInventoryDetailCommand command = warehouseInventoryBuilder.buildQueryProductInventoryDetailCommand(json);

        Page<PageInventoryDetailVo> page = inventoryService.pageInventoryDetail(command);
        Map<String, Object> data = new HashMap<String, Object>();

        if (page != null && CollectionUtils.isNotEmpty(page.content)) {
            data.put("list", warehouseInventoryBuilder.buildQueryProductInventoryDetailResult(page.content));
        }
        responseMsg.setData(data);

        return API_ADAPTED_SUCCESS;

    }

    /**
     * 海外仓总库存查询接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryProductInventoryList4Page")
    public String queryProductInventoryList4Page(@ModelAttribute RequestMsg requestMsg,
                                                 @ModelAttribute ResponseMsg responseMsg) {

        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        WarehouseInventoryValidator.validateProductInventoryList(json);
        PageOverseaInventoriesCommand command = warehouseInventoryBuilder.buildProductInventoryListCommand(json);
        Page<PageOverseaInventoryVo> page = inventoryService.pageOverseaInventories(command);

        Map<String, Object> data = new HashMap<String, Object>();

        if (page != null && CollectionUtils.isNotEmpty(page.content)) {
            data.put("list", warehouseInventoryBuilder.buildQueryProductInventoryListResult(page.content, json));
        }

        Map<String, Object> pageMap = getPageParamsForInventoryList4Page(page);
        data.put("page", pageMap.get("page"));
        responseMsg.setData(data);

        return API_ADAPTED_SUCCESS;

    }

    /**
     * 
     */
    private Map<String, Object> getPageParamsForInventoryList4Page(Page pagelist) {

        Map<String, Object> pageMap = new HashMap<String, Object>();

        if (pagelist != null && pagelist.getPageable() != null) {
            pageMap.put("StartRow", pagelist.getPageable().getPageNumber());
            pageMap.put("NumRows", pagelist.getPageable().getPageSize());
            pageMap.put("TotalRows", pagelist.getTotalElements());
        } else {
            pageMap.put("StartRow", 0);
            pageMap.put("NumRows", 0);
            pageMap.put("TotalRows", 0);
        }

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("page", pageMap);
        return data;
    }

}
